package com.ruoyi.hotel.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.ScheduleConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.hotel.domain.HotelRoomChange;
import com.ruoyi.hotel.domain.HotelRoomListHistory;
import com.ruoyi.hotel.domain.HotelUser;
import com.ruoyi.hotel.domain.vo.HotelRoomChangeTypeVO;
import com.ruoyi.hotel.enums.HotelRoomChangeTypeEnum;
import com.ruoyi.hotel.mapper.HotelRoomChangeMapper;
import com.ruoyi.hotel.mapper.HotelRoomListHistoryMapper;
import com.ruoyi.hotel.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.HotelRoomMapper;
import com.ruoyi.hotel.domain.HotelRoom;
import com.ruoyi.hotel.service.IHotelRoomService;

/**
 * 酒店房间明细Service业务层处理
 *
 * @author iis
 * @date 2024-03-30
 */
@Service
public class HotelRoomServiceImpl implements IHotelRoomService
{
    @Autowired
    private HotelRoomMapper hotelRoomMapper;

    @Autowired
    private HotelRoomListHistoryMapper hotelRoomListHistoryMapper;

    @Autowired
    private HotelRoomChangeMapper hotelRoomChangeMapper;

    /**
     * 查询酒店房间明细
     *
     * @param id 酒店房间明细主键
     * @return 酒店房间明细
     */
    @Override
    public HotelRoom selectHotelRoomById(Long id)
    {
        HotelRoom hotelRoom = hotelRoomMapper.selectHotelRoomById(id);
        return hotelRoom;
    }


    /**
     * 查询酒店房间明细
     *
     * @param hotelId 酒店ID
     * @param roomName 房间名称
     * @return 酒店房间明细
     */
    public HotelRoom selectHotelRoomByHotelIdAndRoomName(String hotelId,String roomName){
        return hotelRoomMapper.selectHotelRoomByHotelIdAndRoomName(hotelId,roomName);
    }

    /**
     * 查询酒店房间明细列表
     *
     * @param hotelRoom 酒店房间明细
     * @return 酒店房间明细
     */
    @Override
    public List<HotelRoom> selectHotelRoomList(HotelRoom hotelRoom)
    {
        return hotelRoomMapper.selectHotelRoomList(hotelRoom);
    }


    /**
     * 根据酒店ID查询酒店房间信息
     * @param hotelRoom
     * @return 酒店房间明细
     */
    @Override
    public List<HotelRoom> selectHotelRoomByHotelId(HotelRoom hotelRoom)
    {
        List<HotelRoom> hotelRoomList =  hotelRoomMapper.selectHotelRoomListNoPullShelves(hotelRoom);

        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        for(SysRole role : roles){
            if(role.getRoleKey().equals("common")){
                hotelRoomList = hotelRoomList.stream()
                        .map(room -> {
                            room.setRoomNum(0L);
                            return room; // 返回更新后的用户对象
                        })
                        .collect(Collectors.toList()); // 收集打码后的用户列表
            }
        }

        return hotelRoomList;
    }


    /**
     * 新增酒店房间明细
     *
     * @param hotelRoom 酒店房间明细
     * @return 结果
     */
    @Override
    public int insertHotelRoom(HotelRoom hotelRoom)
    {
        hotelRoom.setCreateTime(DateUtils.getNowDate());
        return hotelRoomMapper.insertHotelRoom(hotelRoom);
    }

    /**
     * 修改酒店房间明细
     *
     * @param hotelRoom 酒店房间明细
     * @return 结果
     */
    @Override
    public int updateHotelRoom(HotelRoom hotelRoom)
    {
        hotelRoom.setUpdateTime(DateUtils.getNowDate());
        return hotelRoomMapper.updateHotelRoom(hotelRoom);
    }

    /**
     * 批量删除酒店房间明细
     *
     * @param ids 需要删除的酒店房间明细主键
     * @return 结果
     */
    @Override
    public int deleteHotelRoomByIds(Long[] ids)
    {
        return hotelRoomMapper.deleteHotelRoomByIds(ids);
    }

    /**
     * 删除酒店房间明细信息
     *
     * @param id 酒店房间明细主键
     * @return 结果
     */
    @Override
    public int deleteHotelRoomById(Long id)
    {
        return hotelRoomMapper.deleteHotelRoomById(id);
    }



    /**
     * 查询酒店房间统计数据
     *
     * @param hotelRoom 酒店房间
     * @return 酒店房间明细集合
     */
    public Map selectHotelRoomAnalysis(HotelRoom hotelRoom){
        Map returnMap = new HashMap();
        String beginTime = hotelRoom.getParams().get("beginTime").toString();
        String endTime = hotelRoom.getParams().get("endTime").toString();


        Map priceChange = new HashMap<>();
        Map beforeChange = new HashMap<>();
        //查询一天的数据
        if(beginTime.equals(endTime)){

            //取beginTime 前一天的日期
            String beforeTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.addDays(DateUtils.parseDate(beginTime),-1));


            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            //查询当天的数据
            HotelRoomListHistory hotelRoomListHistory = new HotelRoomListHistory();
            hotelRoomListHistory.setHotelId(hotelRoom.getHotelId());
            hotelRoomListHistory.setRoomId(hotelRoom.getId());
            hotelRoomListHistory.setParams(hotelRoom.getParams());

            List<HotelRoomListHistory> queryHotelRoomListHistoryList = hotelRoomListHistoryMapper.selectHotelRoomListHistoryList(hotelRoomListHistory);

            String[] priceChangeTime = new String[queryHotelRoomListHistoryList.size()];
            String[] priceChangeValue = new String[queryHotelRoomListHistoryList.size()];

            for(int i=0;i<queryHotelRoomListHistoryList.size();i++){
                priceChangeTime[i] = sdf.format(queryHotelRoomListHistoryList.get(i).getCreateTime());
                priceChangeValue[i] = queryHotelRoomListHistoryList.get(i).getPrice().toString();
            }

            priceChange.put("key",priceChangeTime);
            priceChange.put("value",priceChangeValue);



            //查询前一天的数据
            hotelRoomListHistory.getParams().put("beginTime",beforeTime);
            hotelRoomListHistory.getParams().put("endTime",beforeTime);

            List<HotelRoomListHistory>  beforeHotelRoomListHistoryList = hotelRoomListHistoryMapper.selectHotelRoomListHistoryList(hotelRoomListHistory);

            String[] beforePriceChangeTime = new String[beforeHotelRoomListHistoryList.size()];
            String[] beforePriceChangeValue = new String[beforeHotelRoomListHistoryList.size()];

            for(int i=0;i<beforeHotelRoomListHistoryList.size();i++){
                beforePriceChangeTime[i] = sdf.format(beforeHotelRoomListHistoryList.get(i).getCreateTime());
                beforePriceChangeValue[i] = beforeHotelRoomListHistoryList.get(i).getPrice().toString();
            }

            beforeChange.put("key",beforePriceChangeTime);
            beforeChange.put("value",beforePriceChangeValue);




            HotelRoomChange hotelRoomChange = new HotelRoomChange();
            hotelRoomChange.setHotelId(hotelRoom.getHotelId());
            hotelRoomChange.setRoomId(hotelRoom.getId());
            hotelRoomChange.setParams(hotelRoom.getParams());
            List<HotelRoomChangeTypeVO> HotelRoomChangeTypeVOList = hotelRoomChangeMapper.selectHotelRoomChangeTypeCount(hotelRoomChange);

            List<Map> statusChangeList = new ArrayList<>();
            for(int i=0;i<HotelRoomChangeTypeVOList.size();i++){
                Map map = new HashMap();
                map.put("name",HotelRoomChangeTypeEnum.valueOFS(HotelRoomChangeTypeVOList.get(i).getChangeType()).getValue());
                map.put("value",HotelRoomChangeTypeVOList.get(i).getCount());
                map.put("labelText",HotelRoomChangeTypeEnum.valueOFS(HotelRoomChangeTypeVOList.get(i).getChangeType()).getValue() + ":" + HotelRoomChangeTypeVOList.get(i).getCount());
                map.put("nameValue",HotelRoomChangeTypeVOList.get(i).getChangeType().toString());
                statusChangeList.add(map);
            }

            returnMap.put("beforePriceChangeList",beforeChange);
            returnMap.put("priceChangeList",priceChange);
            returnMap.put("statusChangeList",statusChangeList);



        }

        else{
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");

            //查询当天的数据
            //这里为什么会报错空指针异常

            HotelRoomChange hotelRoomChange = new HotelRoomChange();
            hotelRoomChange.setHotelId(hotelRoom.getHotelId());
            hotelRoomChange.setRoomId(hotelRoom.getId());
            hotelRoomChange.setParams(hotelRoom.getParams());
            List<HotelRoomChange> queryHotelRoomChangeList = hotelRoomChangeMapper.selectHotelRoomChangeList(hotelRoomChange);

            String[] priceChangeTime = new String[queryHotelRoomChangeList.size()];
            String[] priceChangeValue = new String[queryHotelRoomChangeList.size()];

            for(int i=0;i<queryHotelRoomChangeList.size();i++){
                priceChangeTime[i] = sdf.format(queryHotelRoomChangeList.get(i).getOpTime());
                priceChangeValue[i] = queryHotelRoomChangeList.get(i).getPrice().toString();
            }

            priceChange.put("key",priceChangeTime);
            priceChange.put("value",priceChangeValue);

            //计算beginTime跟endTime相隔多少天
//            int days = DateUtils.differentDays(DateUtils.parseDate(beginTime),DateUtils.parseDate(endTime));

            int days = TimeUtil.differentDays(beginTime,endTime);
            hotelRoomChange.getParams().put("beginTime",TimeUtil.getDateByNumStr(beginTime,0-days-1));
            hotelRoomChange.getParams().put("endTime",TimeUtil.getDateByNumStr(endTime,0-days-1));

            List<HotelRoomChange> beforeQueryHotelRoomChangeList = hotelRoomChangeMapper.selectHotelRoomChangeList(hotelRoomChange);

            String[] beforePriceChangeTime = new String[beforeQueryHotelRoomChangeList.size()];
            String[] beforePriceChangeValue = new String[beforeQueryHotelRoomChangeList.size()];

            for(int i=0;i<beforeQueryHotelRoomChangeList.size();i++){
                beforePriceChangeTime[i] = sdf.format(beforeQueryHotelRoomChangeList.get(i).getOpTime());
                beforePriceChangeValue[i] = beforeQueryHotelRoomChangeList.get(i).getPrice().toString();
            }

            beforeChange.put("key",beforePriceChangeTime);
            beforeChange.put("value",beforePriceChangeValue);



            hotelRoomChange.getParams().put("beginTime",beginTime);
            hotelRoomChange.getParams().put("endTime",endTime);

            List<HotelRoomChangeTypeVO> HotelRoomChangeTypeVOList = hotelRoomChangeMapper.selectHotelRoomChangeTypeCount(hotelRoomChange);

            List<Map> statusChangeList = new ArrayList<>();
            for(int i=0;i<HotelRoomChangeTypeVOList.size();i++){
                Map map = new HashMap();
                map.put("name",HotelRoomChangeTypeEnum.valueOFS(HotelRoomChangeTypeVOList.get(i).getChangeType()).getValue());
                map.put("value",HotelRoomChangeTypeVOList.get(i).getCount());
                map.put("labelText",HotelRoomChangeTypeEnum.valueOFS(HotelRoomChangeTypeVOList.get(i).getChangeType()).getValue() + ":" + HotelRoomChangeTypeVOList.get(i).getCount());
                map.put("nameValue",HotelRoomChangeTypeVOList.get(i).getChangeType().toString());
                statusChangeList.add(map);
            }

            returnMap.put("beforePriceChangeList",beforeChange);
            returnMap.put("priceChangeList",priceChange);
            returnMap.put("statusChangeList",statusChangeList);
        }



        return returnMap;
    }
}
